<!DOCTYPE html><html lang="zh-Hans"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="vue进一步学习"><meta name="keywords" content="vue laochen"><meta name="author" content="晟松"><meta name="copyright" content="晟松"><title>vue进一步学习 | 晟松</title><link rel="shortcut icon" href="/logo.png"><link rel="stylesheet" href="/css/index.css?version=1.9.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.9.0"><meta name="format-detection" content="telephone=no"><meta http-equiv="x-dns-prefetch-control" content="on"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="dns-prefetch" href="https://hm.baidu.com"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?c34d81d53341febe06a155941e2df16d";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();</script><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容:${query}"}},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  hexoVersion: '5.4.0'
} </script><meta name="generator" content="Hexo 5.4.0"></head><body><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar" data-display="true"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#vue%E7%9A%84B%E7%AB%99%E8%80%81%E9%99%88%E5%AD%A6%E4%B9%A0"><span class="toc-number">1.</span> <span class="toc-text">vue的B站老陈学习</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-%E8%BF%87%E6%B8%A1%E5%8A%A8%E7%94%BB%EF%BC%8C%E4%B8%80%E8%88%AC%E4%BD%BF%E7%94%A8transition%E5%AE%8C%E6%88%90%E3%80%82"><span class="toc-number">1.1.</span> <span class="toc-text">1.过渡动画，一般使用transition完成。</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#2-%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="toc-number">1.2.</span> <span class="toc-text">2.生命周期</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-%E7%BB%84%E4%BB%B6%E6%84%9F%E8%A7%89%E8%BF%98%E6%9C%89%E4%B8%80%E4%BA%9B%E4%B8%9C%E8%A5%BF%E8%A6%81%E5%86%99%E3%80%82"><span class="toc-number">1.3.</span> <span class="toc-text">3.组件感觉还有一些东西要写。</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#4-%E6%8F%92%E6%A7%BD%E3%80%82"><span class="toc-number">1.4.</span> <span class="toc-text">4.插槽。</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#5-%E5%8A%A8%E6%80%81%E7%BB%84%E4%BB%B6%E3%80%82"><span class="toc-number">1.5.</span> <span class="toc-text">5.动态组件。</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#6-%E5%8D%95%E6%96%87%E4%BB%B6%E7%BB%84%E4%BB%B6-vue%E6%96%87%E4%BB%B6"><span class="toc-number">1.6.</span> <span class="toc-text">6.单文件组件(.vue文件)</span></a></li></ol></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="/img/avatar.png"></div><div class="author-info__name text-center">晟松</div><div class="author-info__description text-center">晟松的博客</div><div class="follow-button"><a target="_blank" rel="noopener" href="https://gitee.com/cheng_song">Gitee</a></div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">文章</span><span class="pull-right">52</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">标签</span><span class="pull-right">45</span></a></div></div></div><div id="content-outer"><div id="top-container" style="background-image: url(/img/blog-bg.jpg)"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">晟松</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus">   <a class="site-page" href="/">Home</a><a class="site-page" href="/archives">Archives</a><a class="site-page" href="/tags">Tags</a><a class="site-page" href="/categories">Categories</a></span><span class="pull-right"><a class="site-page social-icon search"><i class="fa fa-search"></i><span> 搜索</span></a></span></div><div id="post-info"><div id="post-title">vue进一步学习</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2020-07-13</time><div class="post-meta-wordcount"><span>字数总计: </span><span class="word-count">3.2k</span><span class="post-meta__separator">|</span><span>阅读时长: 12 分钟</span></div></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><h4 id="vue的B站老陈学习"><a href="#vue的B站老陈学习" class="headerlink" title="vue的B站老陈学习"></a>vue的B站老陈学习</h4><p>那些基本的在vue初学习大多都说了，这一篇记一些没说到的。</p>
<h5 id="1-过渡动画，一般使用transition完成。"><a href="#1-过渡动画，一般使用transition完成。" class="headerlink" title="1.过渡动画，一般使用transition完成。"></a>1.过渡动画，一般使用transition完成。</h5><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">transition</span> <span class="attr">name</span> = <span class="string">&quot;nameoftransition&quot;</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">div</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">transition</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>需要指定这个动画的名字(这里是fade)。</p>
<p><strong>重点是css里面的css类名和css动画效果</strong></p>
<p>一般是动画名(例如fade)再加一些特定标识，如</p>
<figure class="highlight css"><table><tr><td class="code"><pre><span class="line"><span class="selector-class">.fade-enter-active</span>, <span class="selector-class">.fade-leave-active</span> &#123;</span><br><span class="line">    <span class="attribute">transition</span>: opacity <span class="number">2s</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.fade-enter</span>, <span class="selector-class">.fade-leave-to</span>  &#123;</span><br><span class="line">    <span class="attribute">opacity</span>: <span class="number">0</span>/*opacity表示透明效果*/</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>active表示动画的进行时间，enter和leave-to表示刚进入的效果和最终离开时的效果。还有enter-to和leave这两个表示动画最后的效果和离开开始的过渡状态。</p>
<img src="https://www.runoob.com/wp-content/uploads/2018/06/transition.png" alt="img" style="zoom:50%;" />

<p>还可以使用自定义的类名，可以和<strong>第三方css类插件库</strong>(如:animate.css)进行配合。</p>
<p>如：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">transition</span></span></span><br><span class="line"><span class="tag">    <span class="attr">name</span>=<span class="string">&quot;custom-classes-transition&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">enter-active-class</span>=<span class="string">&quot;animated tada&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">leave-active-class</span>=<span class="string">&quot;animated bounceOutRight&quot;</span></span></span><br><span class="line"><span class="tag">&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">transition</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>此外还有enter-class,leave-class,enter-to-class,leave-to-class.与上面相对应。</p>
<p>过渡时间你也可以在transition里面是绑定duration进行设置。如：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">transition</span> <span class="attr">:duration</span>=<span class="string">&quot;1000&quot;</span>&gt;</span>...<span class="tag">&lt;/<span class="name">transition</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">transition</span> <span class="attr">:duration</span>=<span class="string">&quot;&#123; enter: 500, leave: 800 &#125;&quot;</span>&gt;</span>...<span class="tag">&lt;/<span class="name">transition</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><strong>过渡模式</strong>：<strong>mode</strong>，在transition标签里面，值为<strong>in-out</strong>和<strong>out-in</strong></p>
<p><strong>多组件过渡</strong>：使用动态组件(is的值来进行切换组件)，在这上面变换css动画</p>
<p><strong>列表过渡</strong>：使用 <code>&lt;transition-group&gt;</code> 组件。</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&lt;transition-group name=&quot;list-complete&quot; tag=&quot;p&quot;&gt;</span><br><span class="line">    &lt;span</span><br><span class="line">      v-for=&quot;item in items&quot;</span><br><span class="line">      v-bind:key=&quot;item&quot;</span><br><span class="line">      class=&quot;list-complete-item&quot;</span><br><span class="line">    &gt;</span><br><span class="line">      &#123;&#123; item &#125;&#125;</span><br><span class="line">    &lt;/span&gt;</span><br><span class="line">  &lt;/transition-group&gt;</span><br><span class="line">//flip方法移动列表</span><br><span class="line">	shuffle: function () &#123;</span><br><span class="line">      this.items = _.shuffle(this.items)</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p><strong><code>v-move</code> class</strong>，它会在元素的改变定位的过程中应用。像之前的类名一样，可以通过 <code>name</code> attribute 来自定义前缀，也可以通过 <code>move-class</code> attribute 手动设置。例如：**_.shuffle()** 函数，在切换时会进行平滑移动</p>
<p><strong>状态过渡</strong>：使用watch查看这些变化。也可将过渡放在组件里面复用</p>
<h5 id="2-生命周期"><a href="#2-生命周期" class="headerlink" title="2.生命周期"></a>2.生命周期</h5><img src="https://cn.vuejs.org/images/lifecycle.png" alt="Vue å®ä¾çå½å¨æ" style="zoom:33%;" />

<p>我们能拿到的只有红色框里面的东西，中间的都是vue自动完成，我们无法控制。一般直接在vue实例里面添加这些东西进行控制。如：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">new</span> Vue(&#123;</span><br><span class="line">  <span class="attr">data</span>: &#123;</span><br><span class="line">    <span class="attr">a</span>: <span class="number">1</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">created</span>: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;<span class="comment">//进行控制.</span></span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;a is: &#x27;</span> + <span class="built_in">this</span>.a)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>在beforeCreate里面输出this和vue里面的data会有不同，this表示vue实例刚创建，但data里面的数据还没有绑定，会输出undefined，同时methods这些也没有绑定</p>
<p>在beforeCreate和created这中间就是绑定data和methods这些。</p>
<p>created之后就是渲染页面，在渲染之前绑定的一些数据在vue实例里面不会拿到，虽然在浏览器html里面有了。在渲染之后(mounted)就可以拿到这些dom对象。</p>
<p>当数据被修改，也会有两个阶段，在before阶段，虽然数据修改了，但内容还没渲染修改，在updated后，就会重新渲染，内容也改变了。</p>
<p>销毁一般很少调用。</p>
<h5 id="3-组件感觉还有一些东西要写。"><a href="#3-组件感觉还有一些东西要写。" class="headerlink" title="3.组件感觉还有一些东西要写。"></a>3.组件感觉还有一些东西要写。</h5><p>prop传递数据（子组件用来接受父组件传递过来的数据的一个自定义属性）：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;app&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">child</span> <span class="attr">message</span>=<span class="string">&quot;hello!&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">child</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">input</span> <span class="attr">v-model</span>=<span class="string">&quot;parentMsg&quot;</span>&gt;</span>//动态prop传递数据</span><br><span class="line">      <span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">child</span> <span class="attr">v-bind:message1</span>=<span class="string">&quot;parentMsg&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">child</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript"><span class="comment">// 注册</span></span></span><br><span class="line"><span class="javascript">Vue.component(<span class="string">&#x27;child&#x27;</span>, &#123;</span></span><br><span class="line"><span class="javascript">  <span class="comment">// 声明 props</span></span></span><br><span class="line"><span class="javascript">  <span class="attr">props</span>: [<span class="string">&#x27;message&#x27;</span>,<span class="string">&#x27;message1&#x27;</span>],</span></span><br><span class="line"><span class="javascript">  <span class="comment">//props也可以这样，可进行简单的验证</span></span></span><br><span class="line"><span class="javascript">  <span class="attr">props</span>: &#123;</span></span><br><span class="line"><span class="javascript">    <span class="comment">// 基础的类型检查 (`null` 和 `undefined` 会通过任何类型验证)</span></span></span><br><span class="line"><span class="javascript">    <span class="attr">propA</span>: <span class="built_in">Number</span>,</span></span><br><span class="line"><span class="javascript">    <span class="comment">// 多个可能的类型</span></span></span><br><span class="line"><span class="javascript">    <span class="attr">propB</span>: [<span class="built_in">String</span>, <span class="built_in">Number</span>],</span></span><br><span class="line"><span class="javascript">    <span class="comment">// 必填的字符串</span></span></span><br><span class="line"><span class="javascript">    <span class="attr">propC</span>: &#123;</span></span><br><span class="line"><span class="javascript">      <span class="attr">type</span>: <span class="built_in">String</span>,</span></span><br><span class="line"><span class="javascript">      <span class="attr">required</span>: <span class="literal">true</span></span></span><br><span class="line"><span class="javascript">    &#125;,</span></span><br><span class="line"><span class="javascript">    <span class="comment">// 带有默认值的数字</span></span></span><br><span class="line"><span class="javascript">    <span class="attr">propD</span>: &#123;</span></span><br><span class="line"><span class="javascript">      <span class="attr">type</span>: <span class="built_in">Number</span>,</span></span><br><span class="line"><span class="javascript">      <span class="attr">default</span>: <span class="number">100</span></span></span><br><span class="line"><span class="javascript">    &#125;,</span></span><br><span class="line"><span class="javascript">    <span class="comment">// 带有默认值的对象</span></span></span><br><span class="line"><span class="javascript">    <span class="attr">propE</span>: &#123;</span></span><br><span class="line"><span class="javascript">      <span class="attr">type</span>: <span class="built_in">Object</span>,</span></span><br><span class="line"><span class="javascript">      <span class="comment">// 对象或数组默认值必须从一个工厂函数获取</span></span></span><br><span class="line"><span class="javascript">      <span class="attr">default</span>: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">return</span> &#123; <span class="attr">message</span>: <span class="string">&#x27;hello&#x27;</span> &#125;</span></span><br><span class="line"><span class="javascript">      &#125;</span></span><br><span class="line"><span class="javascript">    &#125;,</span></span><br><span class="line"><span class="javascript">    <span class="comment">// 自定义验证函数</span></span></span><br><span class="line"><span class="javascript">    <span class="attr">propF</span>: &#123;</span></span><br><span class="line"><span class="javascript">      <span class="attr">validator</span>: <span class="function"><span class="keyword">function</span> (<span class="params">value</span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 这个值必须匹配下列字符串中的一个</span></span></span><br><span class="line"><span class="javascript">        <span class="keyword">return</span> [<span class="string">&#x27;success&#x27;</span>, <span class="string">&#x27;warning&#x27;</span>, <span class="string">&#x27;danger&#x27;</span>].indexOf(value) !== -<span class="number">1</span></span></span><br><span class="line"><span class="javascript">      &#125;</span></span><br><span class="line"><span class="javascript">    &#125;</span></span><br><span class="line"><span class="javascript">  &#125;</span></span><br><span class="line"><span class="javascript">              </span></span><br><span class="line"><span class="javascript">              </span></span><br><span class="line"><span class="javascript">  <span class="comment">// 同样也可以在 vm 实例中像 &quot;this.message&quot; 这样使用</span></span></span><br><span class="line"><span class="javascript">  <span class="attr">template</span>: <span class="string">&#x27;&lt;span&gt;&#123;&#123; message &#125;&#125;+&#123;&#123;message1&#125;&#125;&lt;/span&gt;&#x27;</span></span></span><br><span class="line"><span class="javascript">&#125;)</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>自定义事件（子组件要把数据传递回去，就需要使用自定义事件）</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&lt;div id=&quot;app&quot;&gt;</span><br><span class="line">    &lt;div id=&quot;counter-event-example&quot;&gt;</span><br><span class="line">      &lt;p&gt;&#123;&#123; total &#125;&#125;&lt;/p&gt;</span><br><span class="line">      &lt;button-counter v-on:increment=&quot;incrementTotal&quot;&gt;&lt;/button-counter&gt;</span><br><span class="line">      &lt;button-counter v-on:increment=&quot;incrementTotal&quot;&gt;&lt;/button-counter&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line"> </span><br><span class="line">&lt;script&gt;</span><br><span class="line">Vue.component(&#x27;button-counter&#x27;, &#123;</span><br><span class="line">  template: &#x27;&lt;button v-on:click=&quot;incrementHandler&quot;&gt;&#123;&#123; counter &#125;&#125;&lt;/button&gt;&#x27;,</span><br><span class="line">  data: function () &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      counter: 0</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;</span><br><span class="line">    incrementHandler: function () &#123;</span><br><span class="line">      this.counter += 1</span><br><span class="line">      this.$emit(&#x27;increment&#x27;)//触发increment事件,在v-on监听里面触发，调用vue实例里面的函数</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;)</span><br><span class="line">new Vue(&#123;</span><br><span class="line">  el: &#x27;#counter-event-example&#x27;,</span><br><span class="line">  data: &#123;</span><br><span class="line">    total: 0</span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;</span><br><span class="line">    incrementTotal: function () &#123;</span><br><span class="line">      this.total += 1</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>

<p>component里面的data必须是一个函数，为了保证每个组件的数据不共用。注意驼峰命名法在html里面需要转换成横线格式。</p>
<p>同时有$parent,$children,$root属性，可以在组件中找到父元素的vue对象，子元素的，根(最外层对象)的这些，之后就可以当做vue对象进行调用方法之类的。</p>
<p>v-model也可以绑定到组件上，默认会利用名为 <code>value</code> 的 prop 和名为 <code>input</code> 的事件，需要在组件的template里面添加相应监听和$emit()调用。</p>
<p><strong>一般使用.vue分开使用组件的话(父组件就是使用组件的，子组件就是定义组件的)</strong></p>
<p><strong>父组件给子组件传递数据：在父组件的子组件标签里面通过v-bind绑定一个<u>特定名</u>和数据。在子组件里使用props声明这个<u>特定名</u>，然后就可以使用这个特定名进行数据使用。</strong></p>
<p><strong>子组件给父组件传递数据和使用父组件的方法：</strong></p>
<p>​    <strong>1.在父组件的子组件标签里面通过v-on监听一个<u>特定名</u>和一个在父组件里面的methods的方法名，在子组件里面自定义一个点击事件之类的，触发在子组件里面自定义的方法，在这个方法里面通过this.$emit(‘<u>特定名</u>‘,’数据’)，就会通过这个特定名调用父组件里面的方法，数据也作为参数传递了过去。</strong></p>
<p>​    <strong>2.在父组件里通过v-bind绑定一个<u>特定名</u>和<u>父组件方法</u>，在子组件里通过props声明这个特定名，然后就可以把这个<u>特定名</u>当做一个方法进行使用</strong></p>
<h5 id="4-插槽。"><a href="#4-插槽。" class="headerlink" title="4.插槽。"></a>4.插槽。</h5><p>整体结构不变，但要插入一些内容。<slot></slot>,新版本(2.6.0)可使用v-slot。一般来说，在自定义组件的标签里面添加内容，是不会被渲染到页面上去的，但slot解决了这个问题。<slot>在组件的<strong>template里面添加</strong>使用，添加之后，当在组件标签里面添加内容时，slot将会被替换掉，内容可被渲染到页面上。一个组件的template最外层只会有一个标签，一般可使用一个div将所有元素进行包裹。</p>
<p><strong>父级模板里的所有内容都是在父级作用域中编译的；子模板里的所有内容都是在子作用域中编译的。</strong></p>
<p>还有<strong>具名插槽</strong>，通过父组件<template v-slot:footer>和子组件<slot name="footer"></slot>使用。v-slot只能在template里面使用</p>
<p><strong>作用域插槽</strong>，访问子组件的内容，将v-slot直接在组件标签上面使用</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&lt;current-user v-slot=&quot;slotProps&quot;&gt;</span><br><span class="line">  &#123;&#123; slotProps.user.firstName &#125;&#125;</span><br><span class="line">&lt;/current-user&gt;</span><br></pre></td></tr></table></figure>

<p>v-slot后面有参数的话，可缩写为 <strong>‘#’</strong>，v-slot:header 可以被重写为  #header</p>
<h5 id="5-动态组件。"><a href="#5-动态组件。" class="headerlink" title="5.动态组件。"></a>5.动态组件。</h5><figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">component</span> <span class="attr">v-bind:is</span>=<span class="string">&quot;currentTabComponent&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">component</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>可使用is属性切换不同的组件。</p>
<p>is用法。其中currentTabComponent是在vue实例里面进行数据定义的，然后在局部定义<strong>组件</strong>和方法进行切换currentTabComponent，在this.$options.components[id]，this表示vue实例，获取到组件实例，之后将component标签替换成这个组件。</p>
<p>一般切换的时候，都会重新渲染组件。当我们不想让其重新渲染的时候，就可以使用<keep-alive>标签。如</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">keep-alive</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">component</span> <span class="attr">v-bind:is</span>=<span class="string">&quot;currentTabComponent&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">component</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">keep-alive</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p> keep-alive要求被切换到的组件都有自己的名字，不论是通过组件的 <code>name</code>选项还是局部/全局注册。</p>
<p>因为一些标签 有一些限制出现的地方，如<li>，为了避免这些情况，在组件中的template使用**单文件组件(.vue文件)**就不会存在这些限制，这也是重点。</p>
<hr>
<p>在每个 <code>new Vue</code> 实例的子组件中，其根实例可以通过 <code>$root</code> property 进行访问其data、methods、computed里面的数据方法</p>
<p>和 <code>$root</code> 类似，<code>$parent</code> property 可以用来从一个子组件访问父组件的实例。它提供了一种机会，可以在后期随时触达父级组件，以替代将数据以 prop 的方式传入子组件的方式。</p>
<hr>
<p>可在子组件标签里面使用<strong>ref</strong>，方便在js里面进行访问。只会在组件渲染完成之后生效，并且它们不是响应式的。</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&lt;base-input ref=&quot;usernameInput&quot;&gt;&lt;/base-input&gt;</span><br><span class="line">//js使用</span><br><span class="line">this.$refs.usernameInput</span><br></pre></td></tr></table></figure>

<hr>
<p><strong>依赖注入</strong> ：用到了两个新的实例选项：<code>provide</code> 和 <code>inject</code>。<code>provide</code> 选项允许我们指定我们想要<strong>提供</strong>给后代组件的数据/方法，简化了$parent获取父组件方法的方式</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">//父组件</span><br><span class="line">provide: function () &#123;</span><br><span class="line">  return &#123;</span><br><span class="line">    getMap: this.getMap,//组件里的method</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">//子组件</span><br><span class="line">inject: [&quot;getMap&quot;],//或去父组件的provide</span><br></pre></td></tr></table></figure>

<hr>
<h5 id="6-单文件组件-vue文件"><a href="#6-单文件组件-vue文件" class="headerlink" title="6.单文件组件(.vue文件)"></a>6.<strong>单文件组件(.vue文件)</strong></h5><p>脚手架。.vue文件解决了<strong>全局定义</strong> <strong>字符串模板</strong> <strong>不支持组件 CSS</strong>  <strong>没有构建步骤</strong>这些缺点</p>
<p>1.解决了<strong>全局定义</strong> <strong>字符串模板</strong> <strong>不支持组件 CSS</strong>  <strong>没有构建步骤</strong>这些缺点，使关注点分离，模块化开发。</p>
<p>2.vue-cli    vue.js的<a target="_blank" rel="noopener" href="https://cli.vuejs.org/zh/guide/prototyping.html">标准开发工具</a></p>
<p>3.安装vue-cli按照官方文档的命令进行就可以了，不要其他的不加@符号的命令，在vscode终端里面目前是需要3版本以上。推荐使用git-bash进行npm下载或者cnpm下载。如果出现了什么命令没找到，注意添加安装路径到环境变量。如果安装好了，在bash里面vue –version没有问题，但在vscode终端里无法使用，将vscode设置成管理员打开。<strong>vue create xxx</strong>可以创建一个项目，同时<strong>vue ui</strong>可以进行更简易化的创建配置操作。</p>
<p>4.在src目录里面开发，使用npm run serve看效果，npm run build打包到dist文件夹，可将dist文件夹部署到服务器，当做平常网站。注意绝对路径(/)，和相对路径(./)</p>
<p>5.将文件模块化，明确文件关系</p>
<p><strong>6.app.vue文件</strong>。核心文件，在template里面写html代码，script里面写js，style里面写css。注意全局使用驼峰命名，防止一些奇怪错误，注意在使用组件时，需要将驼峰命名变为横线。</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div id=&quot;app&quot;&gt;</span><br><span class="line">    &lt;chat-com&gt;&lt;/chat-com&gt;</span><br><span class="line">    &lt;userlist-com&gt;&lt;/userlist-com&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">import chatCom from &#x27;./components/chatcom&#x27;</span><br><span class="line">import userlistCom from &#x27;./components/userlistcom&#x27;</span><br><span class="line"></span><br><span class="line">export default &#123;</span><br><span class="line">  name: &#x27;App&#x27;,</span><br><span class="line">  components: &#123;</span><br><span class="line">    chatCom,</span><br><span class="line">    userlistCom</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style scoped&gt;</span><br><span class="line">#app &#123;</span><br><span class="line">  font-family: Avenir, Helvetica, Arial, sans-serif;</span><br><span class="line">  -webkit-font-smoothing: antialiased;</span><br><span class="line">  -moz-osx-font-smoothing: grayscale;</span><br><span class="line">  text-align: center;</span><br><span class="line">  color: #2c3e50;</span><br><span class="line">  margin-top: 60px;</span><br><span class="line">&#125;</span><br><span class="line">&lt;/style&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>使用import引入自定义组件，并在export default的component里面声明，里面的<strong>数据data是返回一个对象</strong>.</p>
<hr>
<p>报错和解决方法：</p>
<p>1.Newline required at end of file but not found eol-last</p>
<p>在.vue文件的最后一行后面加一个空行，还只能加一个空行，多了不行</p>
<p>2.miss什么什么的，在miss的地方加空格。好吧，这是最考试创建的时候开启了eclint选项，是一个严格语法结构。不能使用tab，空格多了两个。</p>
<p>3.推荐使用vscode的格式化文档(代码格式化为：alt+shift+f)，再对照错误进行修改，字符串使用单引号。</p>
</div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">晟松</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://chengsong.info/2020/07/13/vue进一步学习/">https://chengsong.info/2020/07/13/vue进一步学习/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://chengsong.info">晟松</a>！</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/vue-laochen/">vue laochen</a></div><nav id="pagination"><div class="prev-post pull-left"><a href="/2020/07/15/vue-router/"><i class="fa fa-chevron-left">  </i><span>vue路由</span></a></div><div class="next-post pull-right"><a href="/2020/07/08/vue%E5%88%9D%E5%AD%A6%E4%B9%A0/"><span>vue初学习</span><i class="fa fa-chevron-right"></i></a></div></nav></div></div><footer class="footer-bg" style="background-image: url(/img/blog-bg.jpg)"><div class="layout" id="footer"><div class="copyright">&copy;2020 - 2023 By 晟松</div><div class="framework-info"><span>驱动 - </span><a target="_blank" rel="noopener" href="http://hexo.io"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a target="_blank" rel="noopener" href="https://github.com/Molunerfinn/hexo-theme-melody"><span>Melody</span></a></div><div class="icp"><a target="_blank" rel="noopener" href="http://www.beian.miit.gov.cn/"><span>湘ICP备2020021729号</span></a></div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv"><i class="fa fa-file"></i><span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.9.0"></script><script src="/js/fancybox.js?version=1.9.0"></script><script src="/js/sidebar.js?version=1.9.0"></script><script src="/js/copy.js?version=1.9.0"></script><script src="/js/fireworks.js?version=1.9.0"></script><script src="/js/transition.js?version=1.9.0"></script><script src="/js/scroll.js?version=1.9.0"></script><script src="/js/head.js?version=1.9.0"></script><script src="/js/search/local-search.js"></script><script>if(/Android|webOS|iPhone|iPod|iPad|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
  $('#top-container').addClass('is-mobile')
}</script><div class="search-dialog" id="local-search"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章"></div></div></div><hr><div id="local-search-results"><div id="local-hits"></div><div id="local-stats"><div class="local-search-stats__hr" id="hr"><span>由</span> <a target="_blank" rel="noopener" href="https://github.com/wzpan/hexo-generator-search" style="color:#49B1F5;">hexo-generator-search</a>
 <span>提供支持</span></div></div></div><span class="search-close-button"><i class="fa fa-times"></i></span></div><div class="search-mask"></div></body></html>